package com.system.controller;


import com.system.domain.Student;
import com.system.service.StudentService;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void start() {
        student:while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    insertStudent();
                    break;
                case "2":
                    delete();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    show();
                    break;
                case "5":
                    break student;
                default:
                    System.out.println("您输入数据有误,请重新输入");
                    break;
            }
        }
    }

    private void update() {
        while (true) {
            System.out.println("请输入要修改的学号：(按0退出)");
            String id = sc.next();
            int index = studentService.isExists(id);
            if (id.equals("0")) {
                break;
            } else {
                if (index == -1) {
                    System.out.println("学号不存在");
                } else {
                    Student student = inputStuInfo(id);
                    studentService.updateStu(index,student);
                    System.out.println("修改成功");
                    break;
                }
            }
        }
    }

    private void delete() {
        while (true) {
            System.out.println("请输入要删除的学号：(按0退出)");
            String id = sc.next();
            int index = studentService.isExists(id);
            if (id.equals("0")) {
                break;
            } else {
                if (index == -1) {
                    System.out.println("学号不存在");
                } else {
                    studentService.deleteStu(index);
                    System.out.println("删除成功");
                    break;
                }
            }
        }
    }

    private void show() {
        ArrayList<Student>  list=studentService.show();
        if (list.isEmpty()){
            System.out.println("暂无数据");
        }else {
            System.out.println("学号\t姓名\t年龄\t生日\t");
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                System.out.println(student.getId() + "\t\t" + student.getName() + "\t\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    private void insertStudent() {
        String id;
        while (true) {
            System.out.println("请输入学号:");
            id=sc.next();
            int index=studentService.isExists(id);
            if(index==-1){
               break;
            }else{
                System.out.println("学号已经存在");
            }
        }
        Student student = inputStuInfo(id);
        boolean result=studentService.addStudent(student);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    public Student inputStuInfo(String id){
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        String age = sc.next();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student student = new Student(id,name,age,birthday);
        return student;
    }
}
